package com.weather.data.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.weather.data.dao.UserMapper;
import com.weather.data.entity.DO.User;
import com.weather.data.entity.VO.UserPageVO;
import com.weather.data.service.UserService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author baomidou
 * @since 2024-12-23
 */
@Service
public class UserServiceImp extends ServiceImpl<UserMapper, User> implements UserService {

    @Resource
    private UserMapper userMapper;

    @Override
    public Page<User> getUserByPage(UserPageVO userPageVO) {
        Page<User> userPage = new Page<>(userPageVO.getPageNo(), userPageVO.getPageSize());
        Page<User> page = userMapper.selectPage(userPage, Wrappers.<User>lambdaQuery().like(userPageVO.getName() != null, User::getUsername, userPageVO.getName()).eq(userPageVO.getEnable() != null, User::getEnable, userPageVO.getEnable()).orderByDesc(User::getId));
        return page;
    }


    private final BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();

    @Override
    public Long addUser(User user) {
        user.setPassword(passwordEncoder.encode(user.getPassword()));
        userMapper.insert(user);
        return user.getId();
    }
}
